const express = require('express')
const favicon = require('serve-favicon')
const reactSSR = require('react-dom/server')
const fs = require('fs')
const path = require('path')

const app = express();
app.use(favicon(path.join(__dirname, '../favicon.ico')))
app.use('/public',express.static(path.join(__dirname, '../dist')));
const serverEntry = require('../dist/server-entry').default;
const template = fs.readFileSync(path.join(__dirname, '../dist/index.html'), 'utf8')
app.get('*', (req,res)=>{
  let reactString = reactSSR.renderToString(serverEntry)
  // 将服务端渲染的代码替代到指定位置
  res.send(template.replace('<!-- app -->', reactString))
})

app.listen(2017,()=>{
  console.log(`Server is running, port on 2017`)
})
